<template>
  <section id="userMoneyInfo">
    <div class="user_money_info clearfix">
      <div class="pull_left">
        <img src="../assets/image/icon_yue.png" alt="" width="41" height="41">
      </div>
      <div class="pull_left">
        <h2><template v-if="show">{{type==2?money:dataShow.usable}}</template>
      
        <template v-else>********</template> 
          <span class="xs" v-if="show" @click="operateMoney(true)"></span>
          <span class="yc" v-else @click="operateMoney(false)"></span>
        </h2>
        <p v-if="type==1">可用余额($)</p>
        <p v-else>可提现金额($)</p>
      </div>
    </div>
  </section>
</template>
<script>
import {_getUserInfo,_getUserMoney,_getCanWithdrawMoney} from '../axios/index.js'
export default {
  data() {
    return {
      show:true,
      user:"",
      money:''
    }
  },
  props:{
    'type':{
      type:Number
    },
    'refresh':{
      type:Boolean,
      default:false
    }
  },
  computed:{
     //数据展示集合
    dataShow() {
     return this.$js.userAssetNews();
    },
  },
  methods: {
    operateMoney(bol) {
      if (bol) {
        this.show = false;
        localStorage.setItem('moneyShow',false);
      } else {
        this.show = true;
        localStorage.setItem('moneyShow',true);
      }
    },
    async getUserInfo() {
      let _self = this;
      const res = await _getUserInfo({user_type:1});
      if (res.data.code === 200) {
        _self.user = res.data.data;
        _self.$store.commit("setUser", res.data.data);
        _self.getUserMoney(_self.user.user_type);
      }
    },
    async getUserMoney(userType) {
      const res = await _getUserMoney({ user_type: userType });
      if (res.data.code === 200) {
          this.money = Number(res.data.data.available_money) <= 0 ? '0.00' : Number(res.data.data.available_money).toFixed(2);
      }
    },
    /**
     * 可提现金额
     */
    async getMyMoney() {
      const res = await _getCanWithdrawMoney();
      if (res.data.code == 200) {
        this.money = Number(res.data.data).toFixed(2);
      }
    },
  },
  watch:{
    refresh() {
      if (this.refresh) {
        this.getMyMoney();
      }
    }
  },
  created() {
    if (localStorage.getItem('moneyShow')=='true') {
      this.show = true;
    } else {
      this.show = false;
    }
    if (this.type == 2) {
      // 提现
      this.getMyMoney();
    }
    //  else {
    //   // 1为充值
    //   this.getUserInfo();
    // }
  }
}
</script>
